Skip to content

[popups] Keep focus that moved inside before the initial focus resolves - #6013

Closed
xyrolle wants to merge 1 commit into
mui:masterfrom
xyrolle:fix/initial-focus-keeps-focus-inside
Closed

xyrolle wants to merge 1 commit into
mui:masterfrom
xyrolle:fix/initial-focus-keeps-focus-inside

Conversation

@xyrolle

@xyrolle xyrolle commented Oct 16, 2026

Copy link
Copy Markdown

Fixes #6012

What

FloatingFocusManager resolves its initial focus in a microtask and normally applies it on the next frame (synchronously when a virtual screen-reader press opened the popup). When the microtask finds focus on an element inside the floating element, other than the floating element itself, that focus is now kept:

  • Precedence. The focus takes precedence over the default target and over an explicit initialFocus ref or function. This matches focus that was already inside before the open effect, which is also kept whatever the target. initialFocus={false} and an initialFocus function returning undefined are unchanged, because the microtask returns before this point. Focus on the floating element itself is still moved to the initial target, as before.
  • Element types. Any focused element counts, HTML or SVG. The check is containment; the element is not focused again.
  • The queued request. The initial focus request remains queued, but it now only applies if focus has been lost by the time it runs, meaning activeElement is the body or null (for example because the focused element was removed). It then focuses the resolved initial target, as on master. It is not guaranteed to run: as before, a later enqueueFocus call cancels it, and the existing open-state guard still skips it if the popup has closed. After a virtual screen-reader press the request runs at once instead, and finds the focus still in place.
  • A later move. If focus moves to another element before the request runs, that later focus is respected.

Why

If focus reaches an element inside after the open effect and before the microtask, hadFocusInside is true, and shouldFocus then returns true unconditionally. So the queued request forces focus onto the initial target and the user's next keystrokes go elsewhere.

The neighbouring cases are already handled. Focus inside before the effect is caught by focusAlreadyInsideFloatingEl. Focus that moves inside after the microtask is caught by the focusMovedInside guard from #4775, which only applies when the microtask observed focus outside. This change covers the interval between the two.

The regression tests move focus programmatically in that interval. A reproduction through a native browser interaction has not been verified.

How

A new focusMovedInsideBeforeResolve flag sits next to the existing hadFocusInside. When it is set, shouldFocus returns whether focus has been lost (activeElement is the body or null), instead of true.

Test harness change

test/setupVitest.ts replaces jsdom's requestAnimationFrame with a setTimeout that always returned 0, so cancelAnimationFrame never cancelled anything. It now returns the timer as the handle and cancels it with clearTimeout, as a browser does.

Two useListNavigation grid tests ("focuses previous item … skipping disabled items") failed with this change under the old harness. The cause is jsdom-only:

  • A stale open-time item focus that enqueueFocus cancels in a browser still fired in jsdom.
  • On master, the manager's forced re-focus put focus back.
  • With cancellation working, the stale focus is cancelled and both tests pass.

So these tests do not justify forcing the re-focus in a browser. They pass because the harness now cancels as a browser does.

Overlaps

How it was tested

  • A new describe, "focus that moved inside before the initial focus resolved", runs in jsdom and in Chromium (Vitest browser mode). Five of its tests hold frames in a cancellable queue and run them by hand. One waits for unheld frames instead: native frames in Chromium, and in jsdom the harness's timer-backed requestAnimationFrame described above. Its cases:

    • the focus is kept;
    • it is kept across an unheld frame;
    • it is kept over an explicit ref target;
    • it is kept on an SVG element;
    • the frame yields to a later move made before it;
    • the frame falls back to the initial target if focus is lost by then.

    To run it:

    pnpm test:jsdom FloatingFocusManager -t "focus that moved inside before the initial focus resolved" --run
    pnpm test:chromium FloatingFocusManager -t "focus that moved inside before the initial focus resolved" --run

    On master, 5 of the 6 fail and the fall-back case passes, in both environments. With the fix, all 6 pass in both. In jsdom only (it was not run in Chromium), the first version of this fix, which re-focused the element captured by the microtask, fails the SVG, later-move and lost-focus cases.

  • On the final commit:

    • the full jsdom run (@base-ui/react, @base-ui/utils, docs): 380 files, 9311 passed, 1500 skipped, 0 failed;
    • Chromium, across the popups, dialog, drawer, popover, menu, select and combobox suites: 151 files, 4747 passed, 247 skipped, 0 failed;
    • eslint --max-warnings 0 and prettier --check on the changed files, and pnpm typescript at the root: clean.
  • No CSS changed, so stylelint does not apply.

  • I have followed (at least) the PR section of the contributing guide.

@pkg-pr-new

pkg-pr-new Bot commented Oct 16, 2026

Copy link
Copy Markdown

commit: 387c2ad

@code-infra-dashboard

code-infra-dashboard Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+29B(+0.01%) 🔺+20B(+0.01%)

Details of bundle changes

Performance

Total duration: 376.29 ms -9.82 ms(-2.5%) | Renders: 75 (+0) | Paint: 662.76 ms -23.38 ms(-3.4%)

No significant changes — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Oct 16, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 387c2ad
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ad2272dc107cca045f4206b
😎 Deploy Preview https://deploy-preview-6013--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@xyrolle xyrolle closed this Oct 20, 2026
@zannager zannager added type: bug It doesn't behave as expected. scope: all components Widespread work has an impact on almost all components. labels Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: all components Widespread work has an impact on almost all components. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[popups] Initial focus overrides focus that moved inside the popup before the target resolved

2 participants